package openrr.map;

import openrr.geom.TextureGroup;

public class Tile {
	public boolean isEnforced = false;
	public String type;
	public int rubbleLevel;
	public int erosionLevel;
	public int height;
	
	private int xCoord, yCoord;
	private TextureGroup group;
	private TileSet tileSet;
	private MapManager manager;
	private Tile[][] map;
	
	public Tile(MapManager manager, Tile[][] map, int x, int y)
	{
		xCoord = x;
		yCoord = y;
		this.manager = manager;
		this.map = map;
	}
	public void setHeight(int height)
	{
		//trace("setting height to " + height);
		this.height = height;
	}
	public void setTileSet(TileSet set)
	{
		this.tileSet = set;
		this.type = set.type;
		//System.out.println("setting (" + xCoord + ", " + yCoord + ") to set " + set);
	}
	public void updateGeometry()
	{
		//System.out.println("updating geometry @ (" + xCoord + ", " + yCoord + ")");
		int mapWidth = this.manager.width;
		int mapHeight = this.manager.height;
		
		int[][] area = new int[3][3];
		String[] wall;
		
		
		if(xCoord > 0)
		{
			if(yCoord > 0)
			{
				if(xCoord < mapWidth - 1)
				{
					if(yCoord < mapHeight - 1)
					{
						area[0][0] = map[xCoord - 1][yCoord - 1].height;
						area[0][1] = map[xCoord][yCoord - 1].height;
						area[0][2] = map[xCoord + 1][yCoord - 1].height;
						area[1][0] = map[xCoord - 1][yCoord].height;
						area[1][1] = map[xCoord][yCoord].height;
						area[1][2] = map[xCoord + 1][yCoord].height;
						area[2][0] = map[xCoord - 1][yCoord + 1].height;
						area[2][1] = map[xCoord][yCoord + 1].height;
						area[2][2] = map[xCoord + 1][yCoord + 1].height;
					} else {
						//bottom side
						area[0][0] = map[xCoord - 1][yCoord - 1].height;
						area[0][1] = map[xCoord][yCoord - 1].height;
						area[0][2] = map[xCoord + 1][yCoord - 1].height;
						area[1][0] = map[xCoord - 1][yCoord].height;
						area[1][1] = map[xCoord][yCoord].height;
						area[1][2] = map[xCoord + 1][yCoord].height;
						area[2][0] = 1;
						area[2][1] = 1;
						area[2][2] = 1;
					}
				} else {
					//right side
					if(yCoord == mapHeight - 1)
					{
						area[0][0] = map[xCoord - 1][yCoord - 1].height;
						area[0][1] = map[xCoord][yCoord - 1].height;
						area[0][2] = 1;
						area[1][0] = map[xCoord - 1][yCoord].height;
						area[1][1] = map[xCoord][yCoord].height;
						area[1][2] = 1;
						area[2][0] = 1;
						area[2][1] = 1;
						area[2][2] = 1;
					} else {
						area[0][0] = map[xCoord - 1][yCoord - 1].height;
						area[0][1] = map[xCoord][yCoord - 1].height;
						area[0][2] = 1;
						area[1][0] = map[xCoord - 1][yCoord].height;
						area[1][1] = map[xCoord][yCoord].height;
						area[1][2] = 1;
						area[2][0] = map[xCoord - 1][yCoord + 1].height;
						area[2][1] = map[xCoord][yCoord + 1].height;
						area[2][2] = 1;
					}
				}
			} else {
				//top side
				if(xCoord == mapWidth - 1)
				{
					area[0][0] = 1;
					area[0][1] = 1;
					area[0][2] = 1;
					area[1][0] = map[xCoord - 1][yCoord].height;
					area[1][1] = map[xCoord][yCoord].height;
					area[1][2] = 1;
					area[2][0] = map[xCoord - 1][yCoord + 1].height;
					area[2][1] = map[xCoord][yCoord + 1].height;
					area[2][2] = 1;
				} else {
					area[0][0] = 1;
					area[0][1] = 1;
					area[0][2] = 1;
					area[1][0] = map[xCoord - 1][yCoord].height;
					area[1][1] = map[xCoord][yCoord].height;
					area[1][2] = map[xCoord + 1][yCoord].height;
					area[2][0] = map[xCoord - 1][yCoord + 1].height;
					area[2][1] = map[xCoord][yCoord + 1].height;
					area[2][2] = map[xCoord + 1][yCoord + 1].height;
				}
			}
		} else {
			//left side
			if(yCoord == 0)
			{
				area[0][0] = 1;
				area[0][1] = 1;
				area[0][2] = 1;
				area[1][0] = 1;
				area[1][1] = map[xCoord][yCoord].height;
				area[1][2] = map[xCoord + 1][yCoord].height;
				area[2][0] = 1;
				area[2][1] = map[xCoord][yCoord + 1].height;
				area[2][2] = map[xCoord + 1][yCoord + 1].height;
			} else if(yCoord == mapHeight - 1)
			{
				area[0][0] = 1;
				area[0][1] = map[xCoord][yCoord - 1].height;
				area[0][2] = map[xCoord + 1][yCoord - 1].height;
				area[1][0] = 1;
				area[1][1] = map[xCoord][yCoord].height;
				area[1][2] = map[xCoord + 1][yCoord].height;
				area[2][0] = 1;
				area[2][1] = 1;
				area[2][2] = 1;
			} else {
				area[0][0] = 1;
				area[0][1] = map[xCoord][yCoord - 1].height;
				area[0][2] = map[xCoord + 1][yCoord - 1].height;
				area[1][0] = 1;
				area[1][1] = map[xCoord][yCoord].height;
				area[1][2] = map[xCoord + 1][yCoord].height;
				area[2][0] = 1;
				area[2][1] = map[xCoord][yCoord + 1].height;
				area[2][2] = map[xCoord + 1][yCoord + 1].height;
			}
		}
		this.updateGeometryByArea(area);
	}
	private void updateGeometryByArea(int[][] area)
	{
		TextureGroup group;
		int size = this.manager.TILE_SIZE;
		int x = size * xCoord;
		int y = size * yCoord;
		int mapWidth = this.manager.width;
		int mapHeight = this.manager.height;
		int tileHeight = this.manager.TILE_HEIGHT;
		if(this.height == 1)
		{
			String [] wall = this.manager.soilManager.getWallByArea(area);
			//trace(wall[0]);
			if(wall[0] != null)
			{
				if(wall[0].equals("roof"))
				{
					group = tileSet.getTextureGroupByType("roof");
					group.addPlane(x, y, size, tileHeight);
				} else if(wall[0].equals("basic"))
				{
					//trace("adding a basic wall");
					group = tileSet.getTextureGroupByType("wall");
					//trace(wall[1]);
					group.addBasicWall(wall[1], x, y, size, 0, tileHeight);
				} else if(wall[0].equals("corner"))
				{
					//trace("adding a cornered wall");
					//trace("type: " + tileSet.type);
					group = tileSet.getTextureGroupByType("wallCorner");
					//trace(wall[1]);
					group.addCorneredWall(wall[1], x, y, size, 0, tileHeight);
				} else if(wall[0].equals("inverted"))
				{
					//trace("adding an inverted wall");
					//trace("type: " + tileSet.type);
					group = tileSet.getTextureGroupByType("inverted");
					//trace(wall[1]);
					group.addInvertedWall(wall[1], x, y, size, 0, tileHeight);
				}
			} else {
				//error: unrecognised wall type
			}
		} else {
			if(this.tileSet.type.equals("rubble"))
			{
				group = tileSet.getTextureGroupByType("step1");
			} else {
				group = tileSet.getTextureGroupByType("ground");
			}
			group.addPlane(x, y, size, 0);
		}
	}
	public void trace(String str)
	{
		System.out.println(str);
	}
}
